﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HandyControl.Controls;

namespace weighing
{
    /// <summary>
    /// 称重单位设置 
    /// </summary>
    public partial class UnitSettingW : GlowWindow
    {
        private bool isInitValue = true;//初始化值
        private int OldSelectedIndex;
        public UnitSettingW()
        {
            InitializeComponent();

            CommonFunction.ShowCustomIcon(this);
        }
        #region window event
        private void GlowWindow_ContentRendered(object sender, EventArgs e)
        {
            if (App.Config.WeighingConfig.Unit == Unit.Tone)
            {
                this.unitCb.SelectedIndex = 0;
                OldSelectedIndex = 0;
            }
            else if (App.Config.WeighingConfig.Unit == Unit.KG) {
                this.unitCb.SelectedIndex = 1;
                OldSelectedIndex =1;
            }
            else
            {
                this.unitCb.SelectedIndex = -1;
                OldSelectedIndex = -1;
            }

            if (App.Config.WeighingConfig.Digits == 0)
            {
                this.zeroDigitsRb.IsChecked = true;
            }
            else if (App.Config.WeighingConfig.Digits == 1) {
                this.oneDigitsRb.IsChecked = true;
            } 
            else if (App.Config.WeighingConfig.Digits == 2) {
                this.twoDigitsRb.IsChecked = true;
            } 
            else if (App.Config.WeighingConfig.Digits == 3) {
                this.threeDigitsRb.IsChecked = true;
            } else { 
                this.twoDigitsRb.IsChecked=true;
            }
            isInitValue = false;
        }
        private void GlowWindow_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Escape) this.Close();
        }
        #endregion

        #region 过磅单位
  


        private void UnitCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(isInitValue) return;
            int index =this.unitCb.SelectedIndex;
            if(index == 0)
            {
                //原来是 Kg 
                if (App.Config.WeighingConfig.Unit == Unit.KG)
                {
                    if (WeighingBillDao.Instance.HasDatas()) {
                        string msg = $"原来的过磅单位，已经有过磅数据。\n 是否要删除原来的数据？";
                        var res =   CommonFunction.Ask(msg, this);
                        if(res == MessageBoxResult.Yes)
                        {
                           int r = WeighingBillDao.Instance.SoftDeleteAll();
                           if(r > 0)
                            {
                                DoChange(index, Unit.Tone);
                                return;
                            }                         
                        }
                    }
                    else
                    {
                        //没有数据
                        DoChange(index, Unit.Tone);
                        return;
                    }
                    this.unitCb.SelectedIndex = OldSelectedIndex;
                }
                else
                {
                   
                }
            }
            else if (index == 1)
            {
                //原来是 Kg 
                if (App.Config.WeighingConfig.Unit == Unit.Tone)
                {
                    if (WeighingBillDao.Instance.HasDatas())
                    {
                        string msg = $"原来的过磅单位，已经有过磅数据。\n 是否要删除原来的数据？";
                        var res = CommonFunction.Ask(msg, this);
                        if (res == MessageBoxResult.Yes)
                        {
                            int r = WeighingBillDao.Instance.SoftDeleteAll();
                            if (r > 0)
                            {
                                DoChange(index, Unit.KG);
                                return ;
                            }
                        }
                    }
                    else
                    {
                        //没有数据                       
                        DoChange(index, Unit.KG);
                        return;
                    }
                }
                this.unitCb.SelectedIndex = OldSelectedIndex;
            }
            else
            {
               Growl.Info("未发生任何选择的改变");
            }
        }
        #endregion


        //小数位数
        private void DigitsRb_Checked(object sender, RoutedEventArgs e)
        {
            if(isInitValue )return;
            if (this.zeroDigitsRb.IsChecked == true)
            {
                App.Config.WeighingConfig.Digits = 0;
            }
            if (this.oneDigitsRb.IsChecked == true)
            {
                App.Config.WeighingConfig.Digits = 1;
            }
            if (this.twoDigitsRb.IsChecked == true)
            {
                App.Config.WeighingConfig.Digits = 2;
            }
            if (this.threeDigitsRb.IsChecked == true)
            {
                App.Config.WeighingConfig.Digits = 3;
            }
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }

        private void DoChange(int index, Unit unit)
        {
            OldSelectedIndex = index;
            CommonFunction.AlertInfo("设置成功！");
            App.Config.WeighingConfig.Unit = unit;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
            CommonFunction.UpdateScaleItemUnitText();
        }

    }
}
